<?php
namespace  App\Http\Controllers\Radius;
use  App\Http\Controllers\Controller;

use App\Order;
use App\Rad_user;
use App\Rad_user_account;
use App\Usernew;
use Illuminate\Http\Request;

use App\Common\ErrorDefine;

use App\Radacct;
use App\Radcheck;
use App\Radgroupcheck;
use App\Radreply;
use App\Radusergroup;
use App\Rad_manage;
use App\Radgroupreply;
use App\Rad_users;
use App\Rad_account;
use Illuminate\Support\Facades\Crypt;
use DB;
//use App\Common\C
use App\Line;
use App\Agent;

use App\Common\MyCrypt;

class  CrontabController extends Controller
{


    public function deleteExpiredAccount()
    {
        $time = date('Y-m-d H:i:s',time()-86400);


        $username =  Rad_users::where('qq',' ')->where('mobile',' ')->where('account_type','测试')->where('expired_at','<',$time)->get(['username']);


        if(!empty($username))
        {
           foreach($username as $name)
           {
               $Radcheck  = Radcheck::where('username',$name->username)->delete();
               if($Radcheck)
               {
                   echo "Radcheck 删除 $name->username 成功<br>";
               }else{
                   echo "Radcheck 删除 $name->username 失败<br>";
               }

               $group = Radusergroup::where('username',$name->username)->delete();

               if($group)
               {
                   echo "Radusergroup 删除 $name->username成功<br>";
               }else{
                   echo "Radusergroup 删除 $name->username 失败<br>";
               }


              $radreply =  Radreply::where('username',$name->username)->delete();

               if($radreply)
               {
                   echo "Radreply 删除-- $name->username--- 成功<br>";
               }else{
                   echo "Radreply 删除 --  $name->username--失败<br>";
               }
               $users=  Rad_users::where('username',$name->username)->delete();

               if($users)
               {
                   echo "Rad_users 删除-- $name->username--- 成功<br>";
               }else{
                   echo "Rad_users 删除 --  $name->username--失败<br>";
               }

           }
        }

    }


    public function clearRadacct()
    {
//        DELETE FROM radacct WHERE (acctstoptime IS NULL AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime +60 <  UNIX_TIMESTAMP() ) OR ( acctstoptime IS NOT NULL  )

       $sql="DELETE FROM radacct WHERE (acctstoptime IS NULL AND UNIX_TIMESTAMP(acctstarttime) + acctsessiontime +60 <  UNIX_TIMESTAMP() ) OR ( acctstoptime IS NOT NULL  )";
        $user_number = DB::connection('mysql_concent')->select("$sql");
//        $accounts = Radacct::where(DB::Raw("UNIX_TIMESTAMP(acctstarttime) + acctsessiontime + 60"),'<',DB::Raw("UNIX_TIMESTAMP()"))->orWhere(DB::Raw("acctstoptime IS NOT NULL"))->delete();

    }



    public function updateAccountQuota()
    {
        $where = [];
        $where['test_account_quota'] = 20;
        $where['test_con_max_quota'] = 20;

        Rad_manage::whereNotIn('level',['2','3'])->update($where);

    }


    public function modifyAdminPw()
    {
        $mycrypt = new  MyCrypt();
        $manages = Rad_manage::all();

        foreach ($manages  as  $value)
        {
           $value->password  = $mycrypt->encrypt('niumokeji-agent,'.$value->password);
//           $value->password  = Crypt::decrypt($value->password);
            $value->save();
        }

    }



    // 线路列表
    public function updateLinelist()
    {
        $where['enable_flag'] = 1;

        $lines = Line::where($where)->get();


        foreach($lines as $line)
        {

            $str = explode('.',$line->ip);
            $line->niumoip = $str[0].'.niumoip.com';
            $line->save();

        }

    }

}















